/**
 * MainMenu
 * 
 * 03/11/2013
 * 
 * Copyright GPL
 */
package com.jtmproject;

import com.jtmproject.adapters.MenuListAdapter;
import com.jtmproject.classes.ListMenuItem;
import com.jtmproject.classes.MenuItem;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;


/**
 * This class is an activity to show the main menu
 * @author Javier Tejedor
 */
public class MainMenu extends Activity implements OnItemClickListener{

	private ListMenuItem listMenuItem;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);

		ListView listView = (ListView)findViewById(R.id.main_list);
		listView.setOnItemClickListener(this);
		listView.setAdapter(new MenuListAdapter(this, chargeListMenuItem()));
	}

	/**
	 * this charges the listMenuItem
	 */
	private ListMenuItem chargeListMenuItem() {
		listMenuItem = new ListMenuItem();
		listMenuItem.clear();

		listMenuItem.add(
				new MenuItem(
						R.drawable.ic_action_user, 
						R.string.two_players, 
						GameActivity.class)
				);
		
		listMenuItem.add(
				new MenuItem(
						R.drawable.ic_action_bluetooth, 
						R.string.create_server, 
						CreateServer.class)
				);
		
		listMenuItem.add(
				new MenuItem(
						R.drawable.ic_action_bluetooth_connected, 
						R.string.join_server, 
						JoinServer.class)
				);

		return listMenuItem;
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//getMenuInflater().inflate(R.menu.main_menu, menu);
		return true;
	}

	/* (non-Javadoc)
	 * @see android.widget.AdapterView.OnItemClickListener#onItemClick(
	 * android.widget.AdapterView, android.view.View, int, long)
	 */
	@Override
	public void onItemClick(
			AdapterView<?> parent, View view, int position, long id) {
		
		switch(parent.getId()){
		case R.id.main_list:
			launchActivity(listMenuItem.get(position).getActivity());
			break;
		
		}
		
	}

	/**
	 * this launches an activity
	 * @param activity
	 */
	private void launchActivity(Class<?> activity){
		startActivity(new Intent(this, activity));
	}
	
}